import { UdModal, UdModalForm, http, UdDatePicker } from "@/lib"
import React from "react"
import apis from '@/apis/SystemSettings'
import { message, Input } from "antd"
import SelfModal from "./SelfModal"

export const udOpenMdelHandel = (type: string, values?, callBack?) => {
    let systemResourceList = ""
    const formLayout = {
        labelCol: { span: 6 },
        wrapperCol: { span: 12 }
    }
    const childValue = (data) => {
        systemResourceList = data
    }
    let items = [
        { id: 'systemName', label: '系统名称', rules: [{ required: true, message: '请输入系统名称！' }], ...formLayout, render: <Input disabled={values ? true : false} /> },
        { id: 'systemCode', label: '系统编号', rules: [{ required: true, message: '请输入系统编号！' }], ...formLayout, render: <Input disabled={values ? true : false} /> },
        {
            id: 'secret', label: '密钥', rules: [{ required: true, message: '请输入密钥！' }], ...formLayout,
            render: <SecretInput type={type} />
        },
        { id: 'beginTime', label: '生效日期', ...formLayout, render: <UdDatePicker style={{ width: '100%' }} disabled={type === 'detail'} />, rules: [{ required: true, message: '请输入生效日期！' }] },
        { id: 'endTime', label: '失效日期', ...formLayout, render: <UdDatePicker style={{ width: '100%' }} disabled={type === 'detail'} />, rules: [{ required: true, message: '请输入失效日期！' }] },
        { id: 'tabel', label: '资源', render: <SelfModal getChildValue={childValue} setData={values?.systemResource} disabled={type === 'detail'}  />, labelCol: { span: 2 }, wrapperCol: { span: 24 } }
    ]
    if (type == 'add') {
        items = items.filter((item: any) => item.id !== 'systemCode')
    }
    let title = type == 'add' ? '新建外部系统API资源' : type == 'edit' ? '编辑外部系统API资源' : '详情外部系统API资源'
    UdModal.open({
        title,
        width: 800,
        content: <UdModalForm
            values={values}
            items={items}
        />,
        onOk: (values) => {
            let newObj = {
                systemResourceList
            }
            let value = Object.assign(newObj, values)
            if (type === 'add') {
                return http.post(apis.resourceAdd, value).then(res => {
                    message.success('操作成功')
                    callBack && callBack()
                })
            } else if (type === 'edit') {
                return http.post(apis.resourceEdit, value).then(res => {
                    message.success('操作成功')
                    callBack && callBack()
                })
            }


        }
    })
}

function SecretInput(props) { //得到model里的密钥
    const { value, onChange } = props
    const handeSetSecret = () => {
        http.get(apis.getsecret).then(res => {
            onChange && onChange(res.data.secret)
            message.success('操作成功')
        })
    }
    return (
        <div className="add-input">
            <Input placeholder="请输入密钥" value={value} disabled />
            {props.type!='detail'&&<div className="add-class" onClick={handeSetSecret}>生成</div>}
        </div>
    )
}

